<?php
/**
 * �应用评论接收页面������
 *
 * @author snowfir mailto:baige@ppstream.com
 * @date 13-1-7 ����10:43
 */
//

include "header.php";

$m = new m;
$_GET['act'] = $_GET['act'] ? $_GET['act'] : "index";
$m->$_GET['act']();
class m
{
    /**
     * @desc 添加评论����
     * ����
     * @return  评论ID（自增）��������
     *
     * @author snowfir mailto:baige@ppstream.com
     * @date 2013-3-19 11:32
     */
    function add() {
        include "validcode/validcode.php";
        $app_id = $_POST['app_id'] ? $_POST['app_id']:'';
        if(!$app_id){
            return $this->jsonResult(false, '', '无此应用!');
        }
        $com_title = $_POST['com_title'] ? $_POST['com_title'] : '';
        $content = $_POST['content'] ? $_POST['content'] : '';
        $rating = $_POST['rating'] ? $_POST['rating'] : 0;
        $yzm = $_POST['yzm'] ? $_POST['yzm'] : '';
        $user_name = '匿名';
        if($_COOKIE['have_comment_'.$app_id] == 2){
            return $this->jsonResult(false, '520', '抱歉，您1分钟之内，只能发表一条评论！');
        }
        if(!$yzm){
            return $this->jsonResult(false, '', '验证码不能为空!');
        }
        $vc = new lib_validcode_validcode();
        if(!$vc->validCryptWord($_COOKIE['validcode'],$yzm)) {
            return $this->jsonResult(false, '1', '验证码错误!');
        }
        if(!$com_title){
            return $this->jsonResult(false, '', '标题不能为空!');
        }
        if(!$rating){
            return $this->jsonResult(false, '', '评分不能为空!');
        }
        if(!$content){
            return $this->jsonResult(false, '', '评论不能为空!');
        }

        $com_title = preg_replace('/[\:\<\>\!\[\]\{\}\(\)\;\\\]/i', '', $com_title);
        $content = preg_replace('/[\:\<\>\!\[\]\{\}\(\)\;\\\]/i', '', $content);

        if(strlen($com_title)>60)
        {
            return $this->jsonResult(false, '', '标题不能超过30个字!');
        }

        if(strlen($content)>400)
        {
            return $this->jsonResult(false, '', '评论不能超过200个字!');
        }
        $conn_mysql = _mysqllogon('Mysql_102');

        //ȡ������
        $sql = "show table status where Name ='ios_apps_comment'";
        $stmt = _mysqlparse($conn_mysql, $sql);
        _mysqlexecute($stmt);
        $_row_id = mysql_fetch_assoc($stmt);
        $com_id = $_row_id['Auto_increment'];

        $url = 'http://admin.keyword.pps.tv/solution.cgi';
        $post_data['BIZ_NAME'] = '应用评论';
        $post_data['CONTENT'] = $com_title.' '.$content;
        $post_data['USER_ID'] = '';
        $post_data['USER_IP'] = '';
        $post_data['ID'] = $com_id;

        $rs_data = _curl($cminfo, $url, $post_data);
        $rs = unserialize($rs_data);

        if (!is_array($rs) || empty($rs['FINNAL_SUCCESS'])) {
            _status(1, VHOST . "(功能执行)", '定时', "web_crontab.php/{$_GET['act']}", "接收方出错:{$rs_data}", VIP);
            return $this->jsonResult(false, '', '评论失败！');
        } else {
            if($rs['FINNAL_STATUS'] == 4){
                return $this->jsonResult(false, '520', '评论中含有不适当的内容，请核实后再发。');
            }
            $status = $rs['FINNAL_STATUS'] == 1 ? 1:10000;
            $sql_i = "insert into ios_apps_comment
                              (id,app_id,rating,com_title,content,user_name,status,add_time)
                              values (:id,:app_id,:rating,:com_title,
                              :content,:user_name,:status,sysdate)";
            $stmt_i = _mysqlparse($conn_mysql, $sql_i);
            _mysqlbindbyname($stmt_i, ':id', $com_id);
            _mysqlbindbyname($stmt_i, ':app_id', $app_id);
            _mysqlbindbyname($stmt_i, ':rating', $rating);
            _mysqlbindbyname($stmt_i, ':com_title',$com_title);
            _mysqlbindbyname($stmt_i, ':content', $content);
            _mysqlbindbyname($stmt_i, ':user_name',$user_name);
            _mysqlbindbyname($stmt_i, ':status', $status);
            $mysqlerror_i = _mysqlexecute($stmt_i);
            if ($mysqlerror_i) {
                return $this->jsonResult(false, '', '评论失败！');
            }

            $banner_show = $_COOKIE['have_comment_'.$app_id] ? $_COOKIE['have_comment_'.$app_id] : 1;
            if ($banner_show == 1) {
                setcookie('have_comment_'.$app_id, '2', time() + 60, '/', 'www.izhushou.cn');
            }
            return $this->jsonResult(true, '', '评论成功，刷新失败！!');
        }
    }

    /**
     * 调用此函数，确保JSON对象格式固定�
     * @param  $success
     * @param string $data
     * @param null $message
     * @return void
     */
    function jsonResult($success, $data = '', $message = null)
    {
        //$message = iconv('gbk', 'utf-8', $message);
        $c = new stdClass();
        /** @noinspection PhpUndefinedFieldInspection */
        $c->success = $success;
        $c->data = $data;
        if ($message !== null) {
            /** @noinspection PhpUndefinedFieldInspection */
            $c->message = $message;
        }
        $s = json_encode($c);
        if (isset($_GET['callback'])) {
            $s = addslashes($s);
            echo $_GET['callback'] . "(\"$s\")";
        } else
            echo $s;
    }
}
?>